﻿using B.MES.Customer.Application.Application;
using B.MES.Customer.Application.IApplication;
using B.MES.Customer.Domain.DTO;
using B.MES.Customer.Domain;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using B.MES.Customer.Infrastructure.IRepository;
using B.MES.Customer.Infrastructure.Repository;

namespace B.MES.Customer.Read.WebApi.Controllers
{
    /// <summary>
    /// RBAC(角色)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleApplication _roleApplication;

        public RoleController(IRoleApplication roleApplication)
        {
            _roleApplication = roleApplication;
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRole()
        {
            try
            {
                return Ok(_roleApplication.GetRole());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 角色显示+查询+分页
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPage(string? roleName, int page, int size)
        {
            PageApiResut<RoleDTO> pageApiResut = new PageApiResut<RoleDTO>();
            Expression<Func<Role, bool>> whereExpression = x => x.IsDelete == false;
            var list = _roleApplication.GetPage(page, size, whereExpression, false, x => x.RoleId);

            pageApiResut.Data = list.Data.Select(x => new RoleDTO
            {
                RoleId = x.RoleId,
                RoleName = x.RoleName,
                RoleDesc = x.RoleDesc,
            }).ToList();
            if (!string.IsNullOrEmpty(roleName))
            {
                whereExpression = x => x.RoleName.Contains(roleName);
            }

            pageApiResut.TotalCount = list.TotalCount;
            return Ok(pageApiResut);
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public IActionResult AddRole(RoleDTO role)
        {
            try
            {
                return Ok(_roleApplication.AddRole(role));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 单个删除角色（真删除）
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete]
        public IActionResult DeleteRole(int roleId)
        {
            try
            {
                return Ok(_roleApplication.DeleteRole(roleId));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult BatchDeleteRoles(List<Role> list)
        {
            return Ok(_roleApplication.BatchDeleteRoles(list));
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="rolePermissionDTO">角色权限</param>
        /// <returns>角色权限的数据</returns>
        [HttpPost]
        public IActionResult AddRolePermission(RolePermissionDTO rolePermissionDTO)
        {
            return Ok(_roleApplication.AddRolePermission(rolePermissionDTO));
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="role">角色信息</param>
        /// <returns>受影响的行数</returns>
        [HttpPut]
        public IActionResult UpdateRole(Role role)
        {
            return Ok(_roleApplication.UpdateRole(role));
        }

        /// <summary>
        /// 根据角色ID 获取对应的权限ID
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleIdByPermissionIds(string roleId)
        {
            try
            {
                return Ok(_roleApplication.GetRoleIdByPermissionIds(roleId));
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
